var prefix = "/sys/drug"
var name;
$().ready(function() {
	getMenuTreeData();
	validateRule();
});
$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function getAllSelectNodes() {
	var ref = $('#menuTree').jstree(true);// 获得整个树
	name = ref.get_selected(true)[0].text
}
function getMenuTreeData() {
	$.ajax({
		type : "GET",
		url : prefix + "/tree",
		success : function(menuTree) {
			loadMenuTree(menuTree);
		}
	});
}
function loadMenuTree(menuTree) {
	$('#menuTree').jstree({
		'core' : {
			'data' : menuTree,
			"check_callback": true,
            'multiple': false,
		},
		"checkbox" : {
			"three_state" : false
		},
		"plugins" : [ "wholerow", "checkbox" ]
	});
	$('#menuTree').jstree().open_all();
}
function save(){
	getAllSelectNodes();
	$('#drugClassCode').val(name);
	$.ajax({
		cache : true,
		type : "POST",
		url : prefix + "/insertBaseDrug",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.res == 1) {
				parent.layer.msg(data.resMsg);
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);
			} else {
				layer.alert(data.resMsg)
			}
		}
	});
}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			drugName : {
				required : true
			},
			component : {
				required : true
			},
			properties : {
				required : true
			},
			functions : {
				required : true
			},
			taboo : {
				required : true
			},
			attention : {
				required : true
			},
			drugInteraction : {
				required : true
			},
			usages : {
				required : true
			},
			adverseReaction : {
				required : true
			},
			validityTerm : {
				required : true
			},
			barCode : {
				required : true
			},
			approvalNumber : {
				required : true
			},
			isHemp : {
				required : true
			},
			isRecipe : {
				required : true
			},
			flag : {
				required : true
			},
			storages : {
				required : true
			},
		},
		messages : {
			drugName : {
				required : icon + "请输入药品姓名",
			},
			component : {
				required : icon + "请输入成分",
			},
			properties : {
				required : icon + "请输入性状",
			},
			functions : {
				required : icon + "请输入功能主治",
			},
			taboo : {
				required : icon + "请输入禁忌",
			},
			attention : {
				required : icon + "请输入注意事项",
			},
			drugInteraction : {
				required : icon + "请输入相互作用",
			},
			usages : {
				required : icon + "请输入用法用量",
			},
			adverseReaction : {
				required : icon + "请输入不良反应",
			},
			validityTerm : {
				required : icon + "请输入有效期",
			},
			barCode : {
				required : icon + "请输入条码",
			},
			approvalNumber : {
				required : icon + "请输入批准文号",
			},
			isHemp : {
				required : icon + "请选择药品是否含麻",
			},
			isRecipe : {
				required : icon + "请选择药品是否处方药",
			},
			flag : {
				required : icon + "请选择状态",
			},
			storages : {
				required : icon + "请输入贮藏",
			},
		}
	})

}